<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>任务列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="40"></el-table-column>
      <el-table-column prop="projectId" label="任务id" width="80"></el-table-column>
      <el-table-column prop="taskName" label="任务名称" width="80"></el-table-column>
      <el-table-column prop="taskType" label="任务类型" width="80"></el-table-column>
      <el-table-column prop="demand" label="任务需求" width="80"></el-table-column>
      <el-table-column prop="taskProgress" label="任务进度" width="80"></el-table-column>
      <el-table-column prop="distributeName" label="派发人" width="80"></el-table-column>
      <el-table-column prop="beDistributeName" label="被派发人" width="80"></el-table-column>
      <el-table-column prop="deadlineTime" label="截止时间" width="80"></el-table-column>
      <el-table-column prop="version" label="版本" width="80"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    <div class="pagination-wrapper">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" background
                     layout="total, sizes, prev, pager, next, jumper"
                     :page-sizes="[5,10,20]"
                     :current-page="5"
                     :total="total"
                     :page-size="5">
      </el-pagination>
    </div>-->
    <!-- 弹出的编辑任务的对话框 -->
    <el-dialog title="修改任务" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="任务id" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.projectId"></el-input>
        </el-form-item>
        <el-form-item label="任务名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.taskName"></el-input>
        </el-form-item>
        <el-form-item label="任务类型" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.demand"></el-input>
        </el-form-item>
        <el-form-item label="任务需求" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="任务进度" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.taskProgress"></el-input>
        </el-form-item>
        <el-form-item label="派发人" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.distributeName"></el-input>
        </el-form-item>
        <el-form-item label="被派发人" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.beDistributeName"></el-input>
        </el-form-item>
        <el-form-item label="截止时间" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.deadlineTime"></el-input>
        </el-form-item>
        <el-form-item label="版本" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.version"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="backPictureList()">返回列表</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
    /*  page: 1,
      pageSize: 5,
      total: 0,*/
      tableData: [],
      dialogFormVisible: false,
      ruleForm:{
        id: '',
        projectId: '',
        taskName: '',
        taskType: '',
        demand: '',
        taskProgress: '',
        distributeName: '',
        beDistributeName: '',
        deadlineTime: '',
        version: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
 /*   handleCurrentChange(val){
      console.log(`当前页: ${val}`);
      this.page = val;
      this.loadTaskList();
    },
    handleSizeChange(val){
      console.log(`每页 ${val} 条`);
      this.page=1;
      this.pageSize = val;
      this.loadTaskList();
    },
    backPictureList(){
      this.page=1;
      this.pageSize=5;
      this.loadPictureList();
    },*/
    submitEdit() {
      let url = 'http://localhost:8989/tasks/' + this.ruleForm.id +'/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadTaskList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        } else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible = false;
          this.loadTaskList();
        }
      });
    },
    handleEdit(task) {
      let message = '您正在尝试编辑【' + task.id + '-' + task.taskName + '】的任务详情……';
      console.log(message);
      let url = 'http://localhost:8989/tasks/' + task.id;
      console.log(url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.ruleForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$message.error(responseBody.message);
          this.loadTaskList();
        }
      });
    },
    handleDelete(task) {
      let url = 'http://localhost:8989/tasks/' + task.id + '/delete';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadTaskList();
        }
      });
    },
    openDeleteConfirm(task) {
      let message = '此操作将永久删除【' + task.taskName + '】相册，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(task);
      }).catch(() => {
      });
    },
    loadTaskList() {
      console.log('loadTaskList');
      let url = 'http://localhost:8989/tasks/select';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    }
  },
  mounted() {
    console.log('mounted');
    this.loadTaskList();
  }
}
</script>